﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ExitGames.Client.Photon;
using ExitGames.Client.Photon.Lite;
using DICE_GS;

public class InputNickNameSceneManager : MonoBehaviour {
	public GameObject referralInfoObject;
	public UILabel loadingText;

	private static Queue<Dictionary<byte, object>> packetQueue;

	void Awake()
	{
		packetQueue = new Queue<Dictionary<byte, object>>();
	}
	
	// Update is called once per frame
	void Update () {
		
		if (isFinishedLoadUserData && Tables.isLoadDone)
		{
			CSceneManager.manager.GoToWaitroomScene();
		}

		while (packetQueue.Count > 0)
		{
			Dictionary<byte, object> tmpPacket = packetQueue.Dequeue();

			switch ((byte)tmpPacket[0])
			{
				case (byte)OperationCodes.SC_LOGIN:
					{
						glog.debug("SC_LOGIN");
                        PC_SC_LOGIN packet = new PC_SC_LOGIN(tmpPacket);
						if (packet.LoginResult == (int)LoginResult.LR_NEW_USER)
						{
							CSceneManager c = GameObject.FindObjectOfType(typeof(CSceneManager)) as CSceneManager;
							c.GotoInputNicknameScene();
						}
						else if (packet.LoginResult == (int)LoginResult.LR_LOGIN_OK)
						{
							CSceneManager.LoginSuccess = true;
							LoadingWindow.window.ShowShortLoading();
					        loadingText.text = CStringTableManager.Instance.GetItemById(1610);
					        RestAPI.singleton.RefreshUserData(ReturnCallback);
                            RestAPI.singleton.GetPVEStageInfo(ReturnCallback);
                            //CNetworkManager.Instance.Disconnect();

                            CSceneManager.NeedToDisconnect = true;                           
						}
						else
						{
							glog.debug("Login Error!");
						}
					}
					break;
				default:
					glog.debug("LoadingSceneManager's Packet Handler Unknown Packet Recieved [" + (byte)tmpPacket[0] + "]");
					break;
			}
		}
	}

	public static bool PacketHandler(Dictionary<byte, object> _packet)
	{
		if (packetQueue != null)
		{
			packetQueue.Enqueue(_packet);
			return true;
		}

		return false;
	}
	
	bool isFinishedLoadUserData = false;
    void ReturnCallback(Hashtable returnData)
    {
        switch ((RestAPIProtocol)returnData["protocol"])
        {
			case RestAPIProtocol.SENDREFERRALINFO:
	            LoadingWindow.window.HideLoading();
				if( returnData.ContainsKey("status") && int.Parse(returnData["status"].ToString()) != 409 )
				{
					referralInfoObject.SetActive(true);
				}
				break;
            case RestAPIProtocol.REFRESHUSERINFO:
                RestAPI.singleton.GetCurrentStaminaInfo(ReturnCallback);
                break;
            case RestAPIProtocol.GETCURRENTSTAMINAINFO:
                loadingText.text = CStringTableManager.Instance.GetItemById(1609);
                RestAPI.singleton.GetMyUnitList(ReturnCallback);
                break;
            case RestAPIProtocol.GETMYUNITLIST:
                loadingText.text = CStringTableManager.Instance.GetItemById(1608);
                RestAPI.singleton.RefreshMyDeckInfo(ReturnCallback);
                break;
            case RestAPIProtocol.REFRESHMYDECKINFO:
                loadingText.gameObject.SetActive(false);
                isFinishedLoadUserData = true;
                break;
            case RestAPIProtocol.PUTUSERDATA:
                break;
            case RestAPIProtocol.GETPVESTAGEINFO:
                break;
        }
    }
}
